﻿using System.Collections.Generic;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class BuffEffective_Shield : BuffEffective
    {
        protected override void OnInitProperty(BuffEffectData rData, BuffComponent rTarget)
        {
            base.OnInitProperty(rData, rTarget);
            var rConfig = new ConfigData();
            if (rData.param.Count > 0)
            {
                rConfig.type = rData.param[0];
            }
            if (rData.param.Count > 1)
            {
                rConfig.param = rData.param[1];
            }
            rData.userData = rConfig;
        }

        protected override void OnActive(BuffEffectContext rContext, BuffEffectData rConfig, int nRuntimeParam)
        {
            base.OnActive(rContext, rConfig, nRuntimeParam);
            var rConfigData = rConfig.userData as ConfigData;
            if (rConfigData == null)
            {
                return;
            }

            var rTarget = rContext.buff.target;
            var nShield = LFloat.zero;
            switch (rConfigData.type)
            {
                case 1:
                    // 公式护盾
                    nShield = Formula.Calculate<LFloat>(rConfigData.param, rContext.buff.maker.unit, rTarget.unit);
                    break;
                case 2:
                    // 传递
                    nShield = nRuntimeParam.ToLFloat();
                break;
            }

            var nUID = rTarget.shield.AddShield(nShield);
            rContext.result = nUID;
        }

        protected override void OnDeActive(BuffEffectContext rContext, BuffEffectData rConfig)
        {
            base.OnDeActive(rContext, rConfig);
            var rTarget = rContext.buff.target;
            var nUID = (uint)rContext.result;
            rTarget.shield.RemoveShield(nUID);
        }

        public class ConfigData : BuffEffectiveConfig
        {
            public int type;
            public int param;
        }

#if UNITY_EDITOR
        public override string ToString(BuffEffectData rConfig)
        {
            return $"护盾, 公式：[{((ConfigData)rConfig.userData).param}]";
        }
#endif
    }
}